package com.github.glideplus.transform;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.Log;

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.bumptech.glide.load.resource.bitmap.TransformationUtils;

import static android.R.attr.left;
import static android.R.attr.track;
import static android.R.attr.width;
import static com.bumptech.glide.load.resource.bitmap.TransformationUtils.PAINT_FLAGS;

/**
 * @author zoudong
 */
public class FillSpace extends BitmapTransformation {
    public enum CropType {
        TOP,
        CENTER,
        BOTTOM
    }

    private CropType mCropType = CropType.CENTER;

    private boolean sameScale = false; //false ---长宽比如不一致   true---长宽比例 取最大的  cropCenter--长宽比例 取最小的

    public FillSpace(Context context) {
        this(context, false);
    }

    public FillSpace(Context context, boolean sameScale) {
        super(context);
        this.sameScale = sameScale;
    }

    public FillSpace(BitmapPool bitmapPool) {
        super(bitmapPool);
    }

    @Override
    public Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
        if (toTransform == null) {
            return null;
        }
        if (toTransform.getWidth() == outWidth && toTransform.getHeight() == outHeight) {
            return toTransform;
        }
        Matrix matrix = new Matrix();
          float widthPercentage = outWidth / (float) toTransform.getWidth();
          float heightPercentage = outHeight / (float) toTransform.getHeight();
        float dx = 0, dy = 0;
        float scale;
        if (sameScale) {
            if (toTransform.getWidth() * outHeight > outWidth * toTransform.getHeight()) {
                scale = (float) outHeight / (float) toTransform.getHeight();
                dx = (outWidth - toTransform.getWidth() * scale) * 0.5f;
            } else {
                scale = (float) outWidth / (float) toTransform.getWidth();
                dy = (outHeight - toTransform.getHeight() * scale) * 0.5f;
            }
//            matrix.setTranslate(dx, dy);
            matrix.setScale(scale,scale);
        } else {
            matrix.setScale(widthPercentage, heightPercentage);
        }



        Bitmap result = pool.get(outWidth, outHeight, getSaleConfig(toTransform));
        if (result == null) {
            result = Bitmap.createBitmap(outWidth, outHeight, getSaleConfig(toTransform));
        }


        // We don't add or remove alpha, so keep the alpha setting of the Bitmap we were given.
        TransformationUtils.setAlpha(toTransform, result);
        Canvas canvas = new Canvas(result);
        Paint paint = new Paint(PAINT_FLAGS);
        canvas.drawBitmap(toTransform, matrix, paint);

        if (toTransform != null && toTransform != result && !pool.put(toTransform)) {
            toTransform.recycle();
        }
        return result;
    }

    private Bitmap.Config getSaleConfig(Bitmap toTransform) {
        return toTransform.getConfig() != null
                ? toTransform.getConfig() : Bitmap.Config.ARGB_8888;
    }

    @Override
    public String getId() {
        return "FillSpace-" + sameScale + ".com.bumptech.glide.load.resource.bitmap";
    }


}
